package com.dream.base.thread;

import android.os.AsyncTask;
import android.os.Looper;

/**
 * @author: WangKunHui
 * Date: 2017/8/18 14:12
 * Desc:
 * E-mail:life_artist@163.com
 */
public class BaseTaskManager {

    protected static final String TAG = "YaoTaskManager";

    private static BaseTaskManager taskManager;

    private BaseTaskManager() {

    }

    public static synchronized BaseTaskManager getInstance() {

        initTaskManager();

        return taskManager;
    }

    private static void initTaskManager() {
        synchronized (BaseTaskManager.class) {
            if (taskManager == null) {
                taskManager = new BaseTaskManager();
            }
        }
    }

    /**
     * 用于写入数据库的异步操作或需要排队执行的异步操作
     *
     * @param exec
     * @param <T>
     */
    public <T> void addTask(final BaseTaskExecutor<T> exec) {
        if (exec == null) {
            return;
        }

        if (!(Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId())) {
            throw new RuntimeException("addTask must run on main thread");
        }

        new AsyncTask<Void, Void, T>() {

            @Override
            protected void onPreExecute() {
                exec.onMainPreExec();
            }

            @Override
            protected T doInBackground(Void... params) {

                T t = null;
                try {
                    t = exec.exec();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return t;
            }

            @Override
            protected void onPostExecute(T result) {
                exec.onMainSuccess(result);
            }

            //onCancelled方法用于在取消执行中的任务时更改UI
            @Override
            protected void onCancelled() {
                exec.onMainFailed();
            }

        }.executeOnExecutor(ThreadPoolManager.getInstance().getTaskExecutor());
    }

    /**
     * 用于异步操作
     *
     * @param exec
     * @param <T>
     */
    public <T> void addTaskPool(final BaseTaskExecutor<T> exec) {
        if (exec == null) {
            return;
        }

        if (!(Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId())) {
            throw new RuntimeException("addTask must run on main thread");
        }

        new AsyncTask<Void, Void, T>() {

            @Override
            protected void onPreExecute() {
                exec.onMainPreExec();
            }

            @Override
            protected T doInBackground(Void... params) {

                T t = null;
                try {
                    t = exec.exec();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return t;
            }

            @Override
            protected void onPostExecute(T result) {
                exec.onMainSuccess(result);
            }

            //onCancelled方法用于在取消执行中的任务时更改UI
            @Override
            protected void onCancelled() {
                exec.onMainFailed();
            }

        }.executeOnExecutor(ThreadPoolManager.getInstance().getTaskExecutor());
    }
}
